import axios from "axios";
// import { config } from "rx-core";
import { Message, MessageBox } from "element-ui";
import router from "../router";
//设置全局axios默认值
// axios.defaults.timeout = 5000; //5000的超时验证
// axios.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";

// //创建一个axios实例
const instance = axios.create();
instance.defaults.headers.post["Content-Type"] =
  "application/json;charset=UTF-8";
// axios.interceptors.request.use = instance.interceptors.request.use;
// axios.interceptors.response.use = instance.interceptors.response.use;

instance.interceptors.request.use(
  config => {
    let header = sessionStorage.getItem("user");
    let headers = JSON.parse(header);
    if (headers) {
      config.headers["X-Access-Token"] = headers.token;
      config.headers["X-Access-Uid"] = headers.id;
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  }
);
// 拦截器
instance.interceptors.response.use(response => {
  // console.log(response);
  switch (response.data.code) {
    case 1001:
      Message.error(response.data.data[0]);
      break;
    case 1003:
      router.replace({
        //跳转到登录页面
        path: "/" // 将跳转的路由path作为参数，登录成功后跳转到该路由
      });
      break;
    case 1005:
      // Message.error("您当前没有访问的权限！");
      MessageBox.alert(
        "<strong>您没有该权限！前往查权限页面申请权限。</strong>",
        "权限提醒",
        {
          dangerouslyUseHTMLString: true
        }
      );
      // router.replace({
      //   //跳转到登录页面
      //   path: "/404" // 将跳转的路由path作为参数，登录成功后跳转到该路由
      // });
      break;
    default:
      break;
  }
  return response.data;
});
export default instance;
